package com.weilive.platform.dao.user;

import java.util.Date;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.weilive.platform.dao.user.mapper.UserInfoDO;
import com.weilive.platform.dao.user.mapper.UserTokenDO;

@Repository
public interface PlatformUserDAO {

	/**
	 * add user info
	 */

	@Insert("insert into user_info (account, email, phone, password, secret, role, status, origin, device, device_uid, channel, channel_uid, cdate, mdate) values (#{info.account}, #{info.email}, #{info.phone}, #{info.password}, #{info.secret}, #{info.role}, #{info.status}, #{info.origin}, #{info.device}, #{info.device_uid}, #{info.channel}, #{info.channel_uid}, #{info.cdate}, #{info.mdate})")
	@Options(useGeneratedKeys = true, keyProperty = "info.user_id")
	public int addUserInfo(@Param("info") UserInfoDO info);

	/**
	 * get user info
	 */

	@Select("select * from user_info where user_id = #{user_id}")
	@ResultMap("com.weilive.platform.dao.user.mapper.UserInfoMap")
	public UserInfoDO getUserByUid(@Param("user_id") long user_id);

	@Select("select * from user_info where account = #{account}")
	@ResultMap("com.weilive.platform.dao.user.mapper.UserInfoMap")
	public UserInfoDO getUserByAccount(@Param("account") String account);

	@Select("select * from user_info where email = #{email}")
	@ResultMap("com.weilive.platform.dao.user.mapper.UserInfoMap")
	public UserInfoDO getUserByEmail(@Param("email") String email);

	@Select("select * from user_info where phone = #{phone}")
	@ResultMap("com.weilive.platform.dao.user.mapper.UserInfoMap")
	public UserInfoDO getUserByPhone(@Param("phone") String phone);

	@Select("select * from user_info where device_uid = #{device_uid}")
	@ResultMap("com.weilive.platform.dao.user.mapper.UserInfoMap")
	public UserInfoDO getUserByDeviceUid(@Param("device_uid") String device_uid);

	@Select("select * from user_info where channel_uid = #{channel_uid}")
	@ResultMap("com.weilive.platform.dao.user.mapper.UserInfoMap")
	public UserInfoDO getUserByChannelUid(@Param("channel_uid") String channel_uid);

	/**
	 * set user info
	 */

	@Update("update user_info set account = #{account}, email = #{email}, phone = #{phone}, password = #{password}, mdate = #{mdate} where user_id = #{user_id}")
	public int bindUser(@Param("user_id") long user_id, @Param("account") String account, @Param("email") String email,
			@Param("phone") String phone, @Param("password") String password, @Param("mdate") Date mdate);

	@Update("update user_info set account = #{account}, mdate = #{mdate} where user_id = #{user_id}")
	public int setUserAccount(@Param("user_id") long user_id, @Param("account") String account,
			@Param("mdate") Date mdate);

	@Update("update user_info set email = #{email}, mdate = #{mdate} where user_id = #{user_id}")
	public int setUserEmail(@Param("user_id") long user_id, @Param("email") String email, @Param("mdate") Date mdate);

	@Update("update user_info set phone = #{phone}, mdate = #{mdate} where user_id = #{user_id}")
	public int setUserPhone(@Param("user_id") long user_id, @Param("phone") String phone, @Param("mdate") Date mdate);

	@Update("update user_info set password = #{password}, mdate = #{mdate} where user_id = #{user_id}")
	public int setUserPassword(@Param("user_id") long user_id, @Param("password") String password,
			@Param("mdate") Date mdate);

	/**
	 * user token
	 */

	@Select("select * from user_token where token = #{token}")
	@ResultMap("com.weilive.platform.dao.user.mapper.UserTokenMap")
	public UserTokenDO getUserToken(@Param("token") String token);

	@Update("replace into user_token (user_id, type, token, mdate, edate) values (#{token.user_id}, #{token.type}, #{token.token}, #{token.mdate}, #{token.edate})")
	public int setUserToken(@Param("token") UserTokenDO token);
}
